﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PersonalAlbum.Data.Objects;

namespace PersonalAlbum.Data.Entities
{
    public class SupportRepository : IRepository<Support>
    {
        private string _connectionString;
        public SupportRepository(string connectionString)
        {
            this._connectionString = connectionString;
        }
        #region IRepository<Section> Members

        public List<Support> GetItems()
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                return ImagesDB.SupportRequest.Select(s =>
                    new Support()
                    {
                        Id = s.Id,
                        Name = s.Name,
                        Body = s.Body,
                        Email = s.Email,
                        IsActive = s.Active,
                        Subject = s.Subject,
                        TimeStamp = s.Timestamp
                    }).ToList();
            }
        }

        public void AddItem(Support item)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                ImagesDal.SupportRequest s = new ImagesDal.SupportRequest()
                {
                    Name = item.Name,
                    Body = item.Body,
                    Timestamp = item.TimeStamp,
                    Subject = item.Subject,
                    Active = item.IsActive,
                    Email = item.Email
                };
                ImagesDB.AddToSupportRequest(s);
                ImagesDB.SaveChanges();
                item.Id = s.Id;
            }
        }

        public void DeleteItem(Support item)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                var old = ImagesDB.SupportRequest.FirstOrDefault(s => s.Id == item.Id);
                if (old != null)
                {
                    ImagesDB.DeleteObject(old);
                    ImagesDB.SaveChanges();
                }
            }
        }

        public void UpdateItem(Support item)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                var old = ImagesDB.SupportRequest.FirstOrDefault(s => s.Id == item.Id);
                if (old != null)
                {
                    old.Email = item.Email;
                    old.Body = item.Body;
                    old.Active = item.IsActive;
                    old.Name = item.Name;
                    old.Subject = item.Subject;
                    ImagesDB.SaveChanges();
                }
            }
        }

        public PersonalAlbum.Data.Objects.Support GetItemById(int id)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                return ImagesDB.SupportRequest.Where(s => s.Id == id).Select(s =>
                    new PersonalAlbum.Data.Objects.Support()
                    {
                        Id = s.Id,
                        Name = s.Name,
                        Body = s.Body,
                        Email = s.Email,
                        IsActive = s.Active,
                        Subject = s.Subject,
                        TimeStamp = s.Timestamp
                    }).SingleOrDefault();
            }
        }

        #endregion
    }
}
